<?php
declare (strict_types = 1);

namespace app\common\validate;

use think\Validate;

class User extends Validate
{
	protected $rule = [
        'username'          => 'require|max:15|unique:user,username',
        'email'             => 'require|max:64|unique:user,email|email',
        'password'          => 'min:6',
        're_password'       => 'confirm:password',
        'mobile'            => 'mobile|unique:user,mobile',
        'nickname'          => 'max:12|unique:user,nickname',
        'avatar'            => 'max:125|isFile',
    ];

    protected $message = [
        'username.require' => '用户名必须',
        'username.max'     => '用户名最多不能超过15个字符',
        'username.unique'  => '用户名已经存在',


        'email.require' => '邮箱必须',
        'email.max'     => '邮箱最多不能超过64个字符',
        'email.email'   => '邮箱格式错误',
        'email.unique'  => '邮箱已被占用',

        //'password.require'      => '密码必须',
        'password.min'          => '密码最小为6个字符',
        //'re_password.min'       => '重复密码必须',
        're_password.confirm'   => '两次密码不一致',


        'mobile.mobile'     => '手机号错误',
        'mobile.unique'     => '手机号已存在',

        'nickname.max'      => '昵称最多12个字符',
        'nickname.unique'   => '昵称已存在',
        'avatar.max'        => '头像连接错误',
        'avatar.isFile'      => '请重新选择图片.',
    ];

    // 自定义验证规则判断分类id是否有效
    protected function isFile($value, $rule, $data=[])
    {
        $file1 = config('filesystem.disks.local.root') . DS . ltrim($value, '\\/');
        $file2 = config('filesystem.disks.public.root') . DS . ltrim($value, '\\/');
        return is_file($file1) || is_file($file2);
    }


}
